# 智弈交易助手系统配置文件

# 基础配置
SYSTEM_NAME = "智弈交易助手"
SYSTEM_VERSION = "1.0.0"
DEBUG_MODE = True
LOG_LEVEL = "INFO"  # DEBUG, INFO, WARNING, ERROR, CRITICAL

# 时间设置
TIMEZONE = "Asia/Shanghai"
DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S"

# 市场数据配置
MARKET_DATA = {
    # 数据源配置
    "sources": [
        {
            "id": "shanghai",
            "name": "上海证券交易所",
            "protocol": "zmq",
            "endpoint": "tcp://127.0.0.1:5555",
            "symbols": ["600000", "600036", "600519"],
            "data_types": ["tick", "bar", "orderbook"],
            "enabled": True
        },
        {
            "id": "shenzhen",
            "name": "深圳证券交易所",
            "protocol": "zmq",
            "endpoint": "tcp://127.0.0.1:5556",
            "symbols": ["000001", "000858", "002594"],
            "data_types": ["tick", "bar", "orderbook"],
            "enabled": True
        },
        {
            "id": "hkex",
            "name": "香港交易所",
            "protocol": "kafka",
            "endpoint": "kafka://127.0.0.1:9092",
            "symbols": ["00700", "00939", "03690"],
            "data_types": ["tick", "bar"],
            "enabled": False
        }
    ],
    
    # 数据处理配置
    "processing": {
        "buffer_size": 10000,  # 数据缓冲区大小
        "max_latency_ms": 5,   # 最大处理延迟(ms)
        "time_frequency": ["15s", "1m", "5m", "15m", "30m", "1h"],  # 处理频率
        "cross_border": {
            "enabled": True,
            "currency_rate_api": "https://api.exchangerate-api.com/v4/latest/CNY",
            "update_interval_sec": 300
        }
    },
    
    # 数据存储配置
    "storage": {
        "type": "timeseries",  # timeseries, database, file
        "compression_rate": 0.85,  # 压缩率
        "retention_days": 365,     # 数据保留天数
        "influxdb": {
            "url": "http://localhost:8086",
            "token": "your_token",
            "org": "finance",
            "bucket": "market_data"
        }
    }
}

# AI分析引擎配置
AI_ENGINE = {
    "models": [
        {
            "id": "lstm_predictor",
            "name": "LSTM时序预测器",
            "type": "time_series",
            "path": "models/lstm_model",
            "update_interval_sec": 3600,
            "params": {
                "lookback_window": 60,
                "hidden_units": 128,
                "dropout_rate": 0.2,
                "learning_rate": 0.001
            }
        },
        {
            "id": "gnn_analyzer",
            "name": "GNN图神经网络分析器",
            "type": "graph",
            "path": "models/gnn_model",
            "update_interval_sec": 86400,
            "params": {
                "num_layers": 3,
                "heads": 4,
                "hidden_dim": 64
            }
        },
        {
            "id": "transformer_news",
            "name": "Transformer新闻分析器",
            "type": "nlp",
            "path": "models/bert_news_model",
            "update_interval_sec": 3600,
            "params": {
                "model_name": "bert-base-chinese",
                "max_length": 256,
                "batch_size": 32
            }
        },
        {
            "id": "market_regime",
            "name": "市场状态检测器",
            "type": "classification",
            "path": "models/regime_model",
            "update_interval_sec": 3600,
            "params": {
                "volatility_window": 20,
                "trend_threshold": 0.5
            }
        },
        {
            "id": "alert_calculator",
            "name": "预警计算器",
            "type": "scoring",
            "update_interval_sec": 60,
            "params": {
                "tech_weight": 0.6,
                "sentiment_weight": 0.25,
                "liquidity_weight": 0.15,
                "alert_threshold": 0.7,
                "high_risk_threshold": 0.85
            }
        }
    ],
    
    # 分析任务配置
    "tasks": {
        "price_prediction": {
            "enabled": True,
            "frequency": "1m",
            "models": ["lstm_predictor"]
        },
        "sector_analysis": {
            "enabled": True,
            "frequency": "5m",
            "models": ["gnn_analyzer"]
        },
        "news_impact": {
            "enabled": True,
            "frequency": "realtime",
            "models": ["transformer_news"]
        },
        "regime_detection": {
            "enabled": True,
            "frequency": "15m",
            "models": ["market_regime"]
        },
        "alert_generation": {
            "enabled": True,
            "frequency": "15s",
            "models": ["alert_calculator"]
        }
    }
}

# 策略引擎配置
STRATEGY_ENGINE = {
    # 条件单引擎配置
    "auto_order": {
        "enabled": True,
        "max_rules": 1000,
        "max_orders_per_second": 100,
        "execution_timeout_ms": 5000,
        "retry_count": 3
    },
    
    # 云端仿真配置
    "cloud_simulator": {
        "enabled": True,
        "simulation_window_ms": 500,  # 触发前0.5秒进行微型回测
        "risk_check_level": "medium",  # low, medium, high
        "max_simulations_per_second": 50
    },
    
    # 智能订单路由配置
    "smart_router": {
        "enabled": True,
        "execution_algorithms": ["MARKET", "VWAP", "TWAP", "ICEBERG", "POV"],
        "default_algorithm": "MARKET",
        "slippage_control": True,
        "market_impact_model": True
    },
    
    # 仓位管理配置
    "position_sizing": {
        "methods": ["fixed", "percent", "risk_based", "volatility", "optimal_f", "kelly", "margin_based"],
        "default_method": "risk_based",
        "max_position_percent": 10.0,  # 单品种最大仓位占比(%)
        "max_total_exposure": 100.0,   # 总暴露度上限(%)
        "risk_per_trade": 0.02         # 每笔交易风险占比(%)
    },
    
    # 执行策略配置
    "execution_strategy": {
        "types": ["MARKET", "LIMIT", "STOP", "STOP_LIMIT", "TRAILING_STOP"],
        "time_in_force": ["DAY", "GTC", "IOC", "FOK"],
        "default_time_in_force": "DAY"
    }
}

# 风控系统配置
RISK_MANAGEMENT = {
    # 三级熔断机制
    "circuit_breaker": {
        "account_level": {
            "enabled": True,
            "loss_threshold": -5.0,  # 账户亏损达到5%触发熔断
            "recovery_time_min": 30  # 熔断后恢复时间(分钟)
        },
        "strategy_level": {
            "enabled": True,
            "max_drawdown": -3.0,  # 策略最大回撤3%
            "consecutive_losses": 5  # 连续亏损5次触发
        },
        "system_level": {
            "enabled": True,
            "market_volatility_threshold": 30.0,  # 市场波动率超过30%触发
            "order_latency_threshold_ms": 500      # 订单延迟超过500ms触发
        }
    },
    
    # 合规校验配置
    "compliance": {
        "enabled": True,
        "exchange_rules": {
            "shanghai": "config/compliance/shanghai_rules.json",
            "shenzhen": "config/compliance/shenzhen_rules.json",
            "hkex": "config/compliance/hkex_rules.json"
        },
        "position_limits": {
            "per_symbol": 1000000,    # 单品种最大持仓
            "per_exchange": 5000000,  # 单交易所最大持仓
            "total": 10000000         # 总持仓上限
        },
        "order_limits": {
            "per_second": 10,
            "per_minute": 100,
            "per_hour": 1000
        }
    },
    
    # 资金管理配置
    "capital_management": {
        "enabled": True,
        "max_leverage": 1.0,  # 最大杠杆倍数
        "margin_call_level": 130.0,  # 追加保证金水平(%)
        "stop_out_level": 120.0,     # 强平水平(%)
        "cash_reserve_ratio": 0.2    # 现金储备比例(20%)
    },
    
    # 审计追踪配置
    "audit_trail": {
        "enabled": True,
        "log_level": "INFO",
        "storage_type": "blockchain",  # database, blockchain
        "retention_days": 180
    }
}

# 执行引擎配置
EXECUTION_ENGINE = {
    # 交易接口配置
    "brokers": [
        {
            "id": "broker1",
            "name": "券商1",
            "type": "ctp",
            "endpoint": "tcp://127.0.0.1:8888",
            "username": "user1",
            "password": "encrypted_password",
            "enabled": True
        },
        {
            "id": "broker2",
            "name": "券商2",
            "type": "xtp",
            "endpoint": "tcp://127.0.0.1:8889",
            "username": "user2",
            "password": "encrypted_password",
            "enabled": False
        }
    ],
    
    # 订单执行配置
    "order_execution": {
        "max_attempts": 3,
        "retry_interval_ms": 100,
        "timeout_ms": 5000,
        "min_order_size": 100,
        "max_order_size": 1000000
    },
    
    # 智能撤单配置
    "smart_cancel": {
        "enabled": True,
        "success_rate_target": 92.0,  # 目标撤单成功率
        "cancel_thresholds": {
            "price_movement": 0.5,  # 价格变动超过0.5%触发撤单
            "orderbook_change": 20.0,  # 订单簿变化超过20%触发撤单
            "time_in_queue": 30.0  # 排队时间超过30秒触发撤单
        }
    }
}

# Web服务配置
WEB_SERVICE = {
    "enabled": True,
    "host": "0.0.0.0",
    "port": 8000,
    "api_version": "v1",
    "cors_origins": ["*"],
    "docs_enabled": True,
    "debug_enabled": False,
    "auth_required": True,
    "jwt_secret": "your_jwt_secret_key",
    "token_expiry_hours": 24
}

# 监控与告警配置
MONITORING = {
    "enabled": True,
    "metrics_collection_interval_sec": 10,
    "alert_channels": ["email", "sms", "in_app"],
    "health_check_endpoint": "/api/health",
    "performance_tracking": {
        "enabled": True,
        "target_latency_ms": 80,
        "target_throughput": 500000  # 50万笔/秒
    }
}

# 安全配置
SECURITY = {
    # 认证配置
    "authentication": {
        "enabled": True,
        "multi_factor": True,
        "biometric_auth": True,  # 生物特征认证
        "password_policy": {
            "min_length": 8,
            "require_uppercase": True,
            "require_lowercase": True,
            "require_digit": True,
            "require_special": True
        }
    },
    
    # 加密配置
    "encryption": {
        "enabled": True,
        "algorithm": "AES-256",
        "key_rotation_days": 90,
        "sensitive_data_fields": ["password", "api_key", "access_token"]
    },
    
    # 访问控制配置
    "access_control": {
        "enabled": True,
        "role_based": True,
        "max_login_attempts": 5,
        "session_timeout_min": 30
    }
}

# 扩展功能配置
EXTENSIONS = {
    "主力资金监控": {
        "enabled": True,
        "large_order_threshold": 1000000,  # 100万以上视为大单
        "tracking_frequency": "15s"
    },
    
    "跨市场套利": {
        "enabled": False,
        "arbitrage_pairs": [
            {"symbol1": "510300", "symbol2": "300ETF", "threshold": 0.005},
            {"symbol1": "600030", "symbol2": "06830", "threshold": 0.01}
        ]
    },
    
    "策略超市": {
        "enabled": False,
        "max_shared_strategies": 1000,
        "rating_system": "sharpe_ratio"
    },
    
    "量子计算接口": {
        "enabled": False,
        "quantum_provider": "ibm_quantum",
        "api_key": "your_quantum_api_key"
    }
}

# 开发与测试配置
DEVELOPMENT = {
    "unit_tests": {
        "enabled": True,
        "coverage_target": 80.0  # 目标测试覆盖率
    },
    
    "integration_tests": {
        "enabled": True,
        "test_environment": "staging"
    },
    
    "performance_tests": {
        "enabled": True,
        "target_throughput": 500000,  # 50万笔/秒
        "target_latency_ms": 80
    },
    
    "backtesting": {
        "enabled": True,
        "max_history_years": 10,
        "target_speed_minutes": 3  # 10年数据回测速度<3分钟
    }
}